home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1991 / number3 / l3.asm < prev    next >
Assembly Source File  |  1991-05-28  |  3KB  |  81 lines

  1. ; Assembly subroutine for Listing 2. Scans through Buffer, of
  2. ; length BufferLength, counting words and updating WordCount as
  3. ; appropriate. BufferLength must be > 0. *CharFlag and *WordCount
  4. ; should equal 0 on the first call. Tested with TASM 2.0.
  5. ; C near-callable as:
  6. ;       void ScanBuffer(char *Buffer, unsigned int BufferLength,
  7. ;               char *CharFlag, unsigned long *WordCount);
  8.  
  9. parms   struc
  10.         dw      2 dup(?)        ;pushed return address & BP
  11. Buffer  dw      ?               ;buffer to scan
  12. BufferLength dw ?               ;length of buffer to scan
  13. CharFlag dw     ?               ;pointer to flag for state of last
  14.                                 ; char processed on entry (0 on
  15.                                 ; initial call). Updated on exit
  16. WordCount dw    ?               ;pointer to 32-bit count of words
  17.                                 ; found (0 on initial call)
  18. parms   ends
  19.  
  20.         .model  small
  21.         .code
  22.         public  _ScanBuffer
  23. _ScanBuffer     proc    near
  24.         push    bp              ;preserve caller's stack frame
  25.         mov     bp,sp           ;set up local stack frame
  26.         push    si              ;preserve caller's register vars
  27.         push    di
  28.  
  29.         mov     si,[bp+Buffer]  ;point to buffer to scan
  30.         mov     bx,[bp+WordCount]
  31.         mov     cx,[bx]         ;get current 32-bit word count
  32.         mov     dx,[bx+2]
  33.         mov     bx,[bp+CharFlag]
  34.         mov     bl,[bx]         ;get current CharFlag
  35.         mov     di,[bp+BufferLength] ;get # of bytes to scan
  36. ScanLoop:
  37.         mov     bh,bl           ;PredCharFlag = CharFlag;
  38.         lodsb                   ;Ch = *BufferPtr++ & 0x7F;
  39.         and     al,7fh          ;strip high bit for word processors
  40.                                 ; that set it as an internal flag
  41.         mov     bl,1            ;assume this is a char; CharFlag = 1;
  42.         cmp     al,'a'          ;it is a char if between a and z
  43.         jb      CheckAZ
  44.         cmp     al,'z'
  45.         jna     IsAChar
  46. CheckAZ:
  47.         cmp     al,'A'          ;it is a char if between A and Z
  48.         jb      Check09
  49.         cmp     al,'Z'
  50.         jna     IsAChar
  51. Check09:
  52.         cmp     al,'0'          ;it is a char if between 0 and 9
  53.         jb      CheckApostrophe
  54.         cmp     al,'9'
  55.         jna     IsAChar
  56. CheckApostrophe:
  57.         cmp     al,27h          ;it is a char if an apostrophe
  58.         jz      IsAChar
  59.         sub     bl,bl           ;not a char; CharFlag = 0;
  60.         and     bh,bh
  61.         jz      ScanLoopBottom  ;if ((!CharFlag) && PredCharFlag) {
  62.         add     cx,1            ;    (WordCount)++;
  63.         adc     dx,0            ;}
  64. IsAChar:
  65. ScanLoopBottom:
  66.         dec     di              ;} while (--BufferLength);
  67.         jnz     ScanLoop
  68.  
  69.         mov     si,[bp+CharFlag]
  70.         mov     [si],bl         ;set new CharFlag
  71.         mov     bx,[bp+WordCount]
  72.         mov     [bx],cx         ;set new word count
  73.         mov     [bx+2],dx
  74.  
  75.         pop     di              ;restore caller's register vars
  76.         pop     si
  77.         pop     bp              ;restore caller's stack frame
  78.         ret
  79. _ScanBuffer     endp
  80.         end
  81.